#!/bin/bash

# Script to install binary files, checking the sha

set -euo pipefail

# handle args
for i in "$@" ; do
  case "${i}" in
    --arm64-sha256=*)
    ARM64_SHA256=${1/--arm64-sha256=/''}
    shift
    ;;
    --amd64-sha256=*)
    AMD64_SHA256=${1/--amd64-sha256=/''}
    shift
    ;;
    --url=*)
    URL=${1/--url=/''}
    shift
    ;;
    --extract-file=*)
    EXTRACT_FILE=${1/--extract-file=/''}
    shift
    ;;
    --target=*)
    TARGET=${1/--target=/''}
    shift
    ;;
  esac
done


# decide on target filename
DIR=${URL//[^0-9A-Za-z]/}
FILENAME=$(basename $URL)

# decide appropriate checksum, based on platform
case $(dpkg --print-architecture) in
  arm64) CHECKSUM=$ARM64_SHA256;;
  amd64) CHECKSUM=$AMD64_SHA256;;
  *) exit 1;;
esac

# fetch file, by URL
mkdir -p $DIR
wget -P $DIR $URL

# perform checksum
echo "$CHECKSUM $DIR/$FILENAME" | sha256sum -c -

ls -l $DIR

# unzip
ARCHIVE_TYPE=$(file --mime-type "$DIR/$FILENAME" | awk -F "/" '{print $NF}')
echo "Archive type: $ARCHIVE_TYPE"
case $ARCHIVE_TYPE in
  zip)  unzip $DIR/$FILENAME -d $DIR;;
  gzip | x-xz) tar xvf $DIR/$FILENAME -C $DIR;;
  *) exit 1;;
esac

# copy file to wherever specified
ls -l $DIR
sudo cp $DIR/${EXTRACT_FILE} ${TARGET}

# make executable
sudo chmod +x ${TARGET}
rm -Rf $DIR
